package edu.washington.cs.pomdb.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Semaphore;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;


import edu.washington.cs.pomdb.User;

public class UserStatFrame extends JFrame{
	
	/**
	 * 
	 */
	private Semaphore semaphore;
	private static final long serialVersionUID = 201202281334L;
	User u;
	boolean isContinued;
	String prefix;
	private JPanel contentPane;
	
	public boolean isContinued(){
		try {
			semaphore.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isContinued;
	}
	private void createAndShowGUI(){
		contentPane = (JPanel)this.getContentPane();
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory.createEtchedBorder());
		contentPane.setBackground(new Color(204, 204, 204));
		
		JTextArea textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.append(u.toString());
		GUI.addComponent(contentPane, textArea, 10, 10, 580, 480);
		JButton exitButton = new JButton();
		exitButton.setText("OK");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				exit_actionPerformed(e);
			}
			})
		;
		GUI.addComponent(contentPane, exitButton, 280, 520, 80, 50);

		this.setTitle(prefix + u.getUserName());
		this.pack();
		this.setLocation(new Point(200, 200));
		this.setSize(new Dimension(600, 600));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setVisible(true);	
	}
	
	private void exit_actionPerformed(ActionEvent e){
		isContinued = false;
		semaphore.release();
		this.dispose();
	}
	
	
	public UserStatFrame(String prefix, User u){
		super();
		this.u = u;
		this.prefix = prefix;
		semaphore = new Semaphore(0);

		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createAndShowGUI();
				}
			});
		} catch (Exception e) { 
			System.err.println("UserStatFrame.createGUI didn't successfully complete");
			e.printStackTrace();
		}
	}
}
